package com.hhkj.api.bank.service.biz.impl;

import cn.hutool.core.util.ObjectUtil;
import com.hhkj.admin.bank.common.component.supplier.BankSupplier;
import com.hhkj.admin.bank.domain.Bank;
import com.hhkj.admin.bank.model.res.BankRes;
import com.hhkj.admin.bank.service.base.BankService;
import com.hhkj.admin.bank.service.extend.BankExtendService;
import com.hhkj.api.bank.common.cont.ApiBankConst;
import com.hhkj.api.bank.service.biz.ApiBankBizService;
import com.hhkj.api.common.session.holder.ApiStudentSessionHolder;
import com.hhkj.api.common.session.holder.model.LoginStudent;
import com.hhkj.api.train.common.cont.ApiTrainConst;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.redis.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
 * ApiBankServiceImpl
 *
 * @author HHKJ-SERVER-ZJ
 * @since 2023/5/25 11:25
 */
@Service
public class ApiBankBizServiceImpl implements ApiBankBizService {
    @Autowired
    private BankExtendService bankExtendService;
    @Autowired
    private BankSupplier bankBizUtils;
    @Autowired
    private RedisCache redisCache;
    @Autowired
    private BankService bankService;

    @Override
    public void clearBankCache(Long courseParentId, Long courseBankTypeId) {
        String bankCacheKeyPrefix = RedisUtils.createKeyPrefix(
                ApiBankConst.REDIS_CACHE_KEY_PREFIX,
                courseParentId.toString(),
                courseBankTypeId.toString());
        redisCache.deleteBatchObject(bankCacheKeyPrefix);

        String trainCacheKeyPrefix = RedisUtils.createKeyPrefix(
                ApiTrainConst.REDIS_CACHE_KEY_PREFIX,
                courseParentId.toString(),
                courseBankTypeId.toString());
        redisCache.deleteBatchObject(trainCacheKeyPrefix);
    }

    @Override
    public void clearBankCache(Long courseParentId, Long courseId, Long courseBankTypeId) {
        String cacheKeyPrefix = RedisUtils.createKeyPrefix(
                ApiBankConst.REDIS_CACHE_KEY_PREFIX,
                courseParentId.toString(),
                courseBankTypeId.toString(),
                courseId.toString());
        redisCache.deleteBatchObject(cacheKeyPrefix);

        String trainCacheKeyPrefix = RedisUtils.createKeyPrefix(
                ApiTrainConst.REDIS_CACHE_KEY_PREFIX,
                courseParentId.toString(),
                courseBankTypeId.toString(),
                courseId.toString());
        redisCache.deleteBatchObject(trainCacheKeyPrefix);
    }


    @Override
    public BankRes getPublishBank(String typeCode) {
        LoginStudent currentStudent = ApiStudentSessionHolder.getLoginStudent();
        String cacheKey = RedisUtils.createKey(
                ApiBankConst.REDIS_CACHE_KEY_PREFIX,
                String.valueOf(currentStudent.getCourseParentId()),
                String.valueOf(currentStudent.getCourseBankTypeId()),
                String.valueOf(currentStudent.getCourseId()),
                typeCode);
        BankRes bankRes = redisCache.getCacheObject(cacheKey);
        if (ObjectUtil.isNotNull(bankRes)) {
            return bankRes;
        }
        bankRes = bankExtendService.getPublishBank(currentStudent.getCourseId(), currentStudent.getCourseBankTypeId(), typeCode);
        bankBizUtils.supplyQuestionList(bankRes);
        bankBizUtils.supplyRule(bankRes);
        redisCache.setCacheObject(cacheKey, bankRes, 1, TimeUnit.DAYS);
        return bankRes;
    }

    @Override
    public BankRes getBankById(Long id) {
        Bank bankPo = bankService.getById(id);
        String cacheKey = RedisUtils.createKey(
                ApiBankConst.REDIS_CACHE_KEY_PREFIX,
                String.valueOf(bankPo.getCourseParentId()),
                String.valueOf(bankPo.getCourseBankTypeId()),
                String.valueOf(bankPo.getCourseId()),
                bankPo.getId().toString(),
                bankPo.getTypeCode());
        BankRes bankRes = redisCache.getCacheObject(cacheKey);
        if (ObjectUtil.isNotNull(bankRes)) {
            return bankRes;
        }
        bankRes = bankExtendService.getResById(id);
        bankBizUtils.supplyQuestionList(bankRes);
        bankBizUtils.supplyRule(bankRes);
        redisCache.setCacheObject(cacheKey, bankRes, 1, TimeUnit.DAYS);
        return bankRes;
    }

    @Override
    public BankRes getBankByParentIdAndTypeCode(Long parentId, String typeCode) {
        BankRes bankRes = bankExtendService.getByParentIdAndTypeCode(parentId, typeCode);
        String cacheKey = RedisUtils.createKey(
                ApiBankConst.REDIS_CACHE_KEY_PREFIX,
                String.valueOf(bankRes.getCourseParentId()),
                String.valueOf(bankRes.getCourseBankTypeId()),
                String.valueOf(bankRes.getCourseId()),
                bankRes.getId().toString(),
                bankRes.getTypeCode());
        BankRes bankResCache = redisCache.getCacheObject(cacheKey);
        if (ObjectUtil.isNotNull(bankResCache)) {
            return bankResCache;
        }
        bankBizUtils.supplyQuestionList(bankRes);
        bankBizUtils.supplyRule(bankRes);
        redisCache.setCacheObject(cacheKey, bankRes, 1, TimeUnit.DAYS);
        return bankRes;
    }
}
